package com.example.demo.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.QueryPageParam;
import com.example.demo.common.Result;
import com.example.demo.entity.Customer;
import com.example.demo.entity.Menu;
import com.example.demo.entity.User;
import com.example.demo.exception.ServiceException;
import com.example.demo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    CustomerService customerService;

    @PostMapping("/listPageC1")
    public Result listPageC1(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        Page<Customer> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Customer> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(Customer::getName,name);
        }

        IPage result = customerService.pageCC(page,lambdaQueryWrapper);

        System.out.println("total=="+result.getTotal());

        return Result.suc(result.getRecords(),result.getTotal());
    }


    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return customerService.removeById(id)?Result.suc():Result.fail();
    }

    @PostMapping("/save")
    public Result save(@RequestBody Customer customer){
        return customerService.save(customer)?Result.suc():Result.fail();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Customer customer){
        return customerService.updateById(customer)?Result.suc():Result.fail();
    }

    @GetMapping("/findByNo")
    public Result findByNo(@RequestParam String no){
        List list = customerService.lambdaQuery().eq(Customer::getNo,no).list();
        return list.size()>0?Result.suc(list):Result.fail();
    }



    @PostMapping("/login")
    public Result login(@RequestBody Customer customer){

  Customer customer1 =  customerService.login(customer);

        HashMap res = new HashMap();
        res.put("user",customer1);

if(customer1==null){
    return Result.fail();
}
        return Result.suc(res);


    }
    @PostMapping("/reset")
    public Result reset(@RequestBody Customer customer) {
        if (StrUtil.isBlank(customer.getName()) || StrUtil.isBlank(customer.getPhone())) {
            throw new ServiceException("-1", "参数异常");
        }
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", customer.getName());
        queryWrapper.eq("phone", customer.getPhone());
        List<Customer> list = customerService.list(queryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            Customer customer1 = list.get(0);
            customer1.setPassword("123");
            customerService.saveOrUpdate(customer1);
        } else {
            return Result.fail(-1, "未找到用户");
        }
        return Result.suc();
    }

    @PostMapping("/register")
    public Result register(@RequestBody Customer customer) {
        String no = customer.getNo();
        String password = customer.getPassword();
        String name = customer.getName();
        String address = customer.getAddress();
        if (StrUtil.isBlank(no) || StrUtil.isBlank(password)||StrUtil.isBlank(name)|| StrUtil.isBlank(address)) {
            return Result.fail(400, "参数错误");
        }
        QueryWrapper<Customer> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("no",no);


        Customer  one = customerService.getOne(queryWrapper); // 从数据库查询用户信息

        if(one == null){
            Customer customer1 = new Customer();
            customer1.setNo(customer.getNo());
            customer1.setName(customer.getName());
            customer1.setPassword(customer.getPassword());
            customer1.setAddress(customer.getAddress());
            customer1.setPhone(customer.getPhone());
            customerService.save(customer1);
        }else{

            return Result.fail(-1, "用户已存在");

        }
        return Result.suc();

    }
    @PostMapping("/changepass")
    public Result change(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String oldpass = (String) param.get("oldpassword");
      String newpass = (String) param.get("newpassword");
      String name = (String)param.get("name");
      QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("password",oldpass);
        queryWrapper.eq("name",name);
   List<Customer> list = customerService.list(queryWrapper);
  Customer customer = list.get(0);
  customer.setPassword(newpass);
  customerService.saveOrUpdate(customer);
  return Result.suc();
    }
}

